
var vue = new Vue({
	
	el : '#material_app',
	mounted(){
	      this.getData();
	  },
	data:{
		
		tip1 : '只能上传是 jpg、png 格式文件!',
		tip2 : '文件大小不能超过 10MB!',
		tip : '只能上传是 jpg、png 格式文件,且文件大小不能超过 10MB!',
		tip4 : '首页上传图片需要选择图片大小!',
		fileList : [],

		items:[{name:"大图(1000 x 424)",id:'1'},{name:"小图(316 x 328)",id:'2'}],
	    datas:[{name:"大图(1000 x 424)",id:'1'},{name:"小图(316 x 328)",id:'2'}],
	    selectcompy : {
	    	  compycode : '',
	    	  compyDesc : ''
			},

		uploadUrl : COMMON_URL.URL+'/material/upload?id=2'+ "&companyId=2",
		whichtabs : "2",
		tableData : [],
		loading: false,
		currentPage : 1,
		pagesize : 10,
		totalSize : 10,
		//url
		queryUrl:COMMON_URL.URL+'material/pageList',
		deleteUrl: COMMON_URL.URL+'material/deleteMateria',
		searchUrl: COMMON_URL.URL+'material/toSearch',
		getcompycode: COMMON_URL.URL+'material/getcompy',
		visible2: false,
		dialogEdit:false,
		dialogAdd:false,
		dialogTitle:'',
		pollingConfig:{
			  id:'',
			  kanbanPoition:'',
			  kanbanUrl :''
		},
		pollingConfigAdd:{
			  kanbanPoition:'',
			  kanbanUrl :''
		},
		searchpolling:{
			  name :'',
			  type :''
		}


	},
	methods : {
		getData:function(){
			vue.items = vue.datas;
			/*var pageNum = 1;
			var pageSize = 10;
			 $.ajax({
					url : this.getcompycode,
					dataType : 'json',
					type : 'get',
					data : {pageNum:pageNum,pageSize:pageSize},
					success : function(data){
						if(data.ok){
							
							
							vue.items =  data.data;
						}
					}
			  });*/
	          

	      },
		// 上传成功后的回调
	    uploadSuccess (response, file, fileList) {
	      this.fileList = fileList;
	      this.loadData(this.currentPage,this.pagesize);
	      this.$notify({
	          title: '成功',
	          message: response,
	          type: 'success'
	        });
	    },
	    choose(compycode){
	    	this.uploadUrl = COMMON_URL.URL+'/material/upload?id=2'+ "&companyId="+compycode;
	    },
	    handleClick(tab, event) {
	    	if("商店精品" == tab.label){
	    		this.uploadUrl = COMMON_URL.URL+'/material/upload?id=1'+ "&companyId="+vue.selectcompy.compycode;
	    		
	    	}else if("首页展示" == tab.label){
	    		this.uploadUrl = COMMON_URL.URL+'/material/upload?id=2'+ "&companyId="+vue.selectcompy.compycode;
	    		this.whichtabs = '2';
	    		
	    	}else if("热销产品" == tab.label){
	    		this.uploadUrl = COMMON_URL.URL+'/material/upload?id=3'+ "&companyId="+vue.selectcompy.compycode;
	    		this.whichtabs = '3';
	    		
	    	}else if("全部产品" == tab.label){
	    		this.uploadUrl = COMMON_URL.URL+'/material/upload?id=4'+ "&companyId="+vue.selectcompy.compycode;
	    		this.whichtabs = '4';
	    	}
	      },
	     
	    // 上传错误
	    uploadError (err, file, fileList) {
	      this.fileList = fileList;
	      this.$notify.error({
	          title: '错误',
	          message: '上传失败，请重试！',
	          duration: 0
	        });
	    },
	    // 上传前对文件的大小/类型的判断
	    beforeAvatarUpload (file) {
	      const extension1 = file.name.split('.')[1] === 'jpg';
	      const extension2 = file.name.split('.')[1] === 'png';
	      const extension3 = file.name.split('.')[1] === 'jpeg';
	      const extension4 = file.name.split('.')[1] === 'PNG';
	      const extension5 = file.name.split('.')[1] === 'JPG';
	      const extension6 = file.name.split('.')[1] === 'JPEG';
	      const isLt2M = file.size / 1024 / 1024 < 12;
	      
	      
	      if((vue.selectcompy.compycode==''||vue.selectcompy.compycode==null)&&this.whichtabs=='2'){
	    	  this.$notify({
		            title: '警告',
		            message: this.tip4,
		            type: 'warning'
		          });
	    	  return false;
	      }
	      
	      
	      if (!extension1 && !extension2 && !extension3
	    		  && !extension4 && !extension5 && !extension6) {
	        this.$notify({
	            title: '警告',
	            message: this.tip1,
	            type: 'warning'
	          });
	      }
	      if (!isLt2M) {
	        this.$notify({
	            title: '警告',
	            message: this.tip2,
	            type: 'warning'
	          });
	      }
	      return extension1 || extension2 || extension3
	      || extension4|| extension5|| extension6&& isLt2M;
	    },
	    

		  
		  dateFormat(row,col){
			  
			  var date = row[col.property];
			  if(date == null){
				  return '';
			  }
			  return moment(date).format('YYYY-MM-DD HH:mm:ss');
		  },
		  //查询数据
		  loadData(pageNum,pageSize){
			  $.ajax({
					url : this.queryUrl,
					dataType : 'json',
					type : 'get',
					data : {pageNum:pageNum,pageSize:pageSize},
					success : function(data){
						if(data.ok){
							var value = data.data.list;
							vue.tableData = value;
							vue.currentPage = data.data.currentPage;
							vue.totalSize = data.data.allCounts;
						}
					}
			  });
		  },
		  //根据条件查询数据
		  toSearch(){
			  var pageNum = 1;
			  var pageSize = 10;
			  $.ajax({
					url : this.searchUrl,
					dataType : 'json',
					type : 'get',
					data : {pageNum:pageNum,pageSize:pageSize,name:vue.searchpolling.name,type:vue.searchpolling.type},
					success : function(data){
						if(data.ok){
							var value = data.data.list;
							vue.tableData = value;
							vue.currentPage = data.data.currentPage;
							vue.totalSize = data.data.allCounts;
						}
					}
			  });
		  },
		  //分页
		  handleSizeChange(val){
			this.pagesize = val;
			this.loadData(this.currentPage,this.pagesize);
		  },
		  //分页
		  handleCurrentChange(val){
			this.currentPage = val;
			this.loadData(this.currentPage,this.pagesize);
		  },
	      //删除看板配置
	      deleteConfig(index,row,value){
			  const h = this.$createElement;
			  this.$msgbox({
				  title : "警告",
				  message : h('p', null, [
		            h('span', null, '你将要删除 '),
		            h('i', { style: 'color: teal' }, row.name),
		            h('span', null, ' ?'),
		          ]),
		          showCancelButton: true,
		          confirmButtonText: '确定',
		          cancelButtonText: '取消',
		          beforeClose: (action, instance, done) => {
		              if (action === 'confirm') {
		                instance.confirmButtonLoading = true;
		                instance.confirmButtonText = '执行中...';
		        		$.ajax({
		        			type : 'get',
		        			dataType : 'json',
		        			data : {id : row.id},
		        			url : this.deleteUrl,
		        			success : function(data){
		        				if(data.ok){
		        					done();
		        					value.splice(index,1);
		        					setTimeout(() => {
		                				instance.confirmButtonLoading = false;
		                			}, 300);
		        					vue.$message({
		        			        	  showClose: true,
		        			              type: 'success',
		        			              message: '操作成功'
		        			          });
		        				}else{
		        					
		        					done();
		        					setTimeout(() => {
		                				instance.confirmButtonLoading = false;
		                			}, 300);
		        					vue.$message({
		        			        	  showClose: true,
		        			              type: 'warning',
		        			              message: data.msg
		        			          });
		    					}
		        			}
		        		});
		              }else{
		                done();
		              }
		          }
			  });
		  }
	   
	    
	    
	    
	}
	
	
});


vue.loadData(vue.currentPage, vue.pagesize);